#include <stm8s_tim2.h>

void TIM2_ICInit(TIM2_Channel_TypeDef TIM2_Channel,
                 TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
                 TIM2_ICSelection_TypeDef TIM2_ICSelection,
                 TIM2_ICPSC_TypeDef TIM2_ICPrescaler,
                 uint8_t TIM2_ICFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
  assert_param(IS_TIM2_IC_POLARITY_OK(TIM2_ICPolarity));
  assert_param(IS_TIM2_IC_SELECTION_OK(TIM2_ICSelection));
  assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_ICPrescaler));
  assert_param(IS_TIM2_IC_FILTER_OK(TIM2_ICFilter));
  
  if (TIM2_Channel == TIM2_CHANNEL_1)
  {
    /* TI1 Configuration */
    TI1_Config((uint8_t)TIM2_ICPolarity,
               (uint8_t)TIM2_ICSelection,
               (uint8_t)TIM2_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  }
  else if (TIM2_Channel == TIM2_CHANNEL_2)
  {
    /* TI2 Configuration */
    TI2_Config((uint8_t)TIM2_ICPolarity,
               (uint8_t)TIM2_ICSelection,
               (uint8_t)TIM2_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
  }
  else
  {
    /* TI3 Configuration */
    TI3_Config((uint8_t)TIM2_ICPolarity,
               (uint8_t)TIM2_ICSelection,
               (uint8_t)TIM2_ICFilter);
    
    /* Set the Input Capture Prescaler value */
    TIM2_SetIC3Prescaler(TIM2_ICPrescaler);
  }
}

/**
  * @brief  Configures the TIM2 peripheral in PWM Input Mode according to the specified parameters.
    * @param    TIM2_Channel specifies the Input Capture Channel from @ref TIM2_Channel_TypeDef.
  * @param   TIM2_ICPolarity specifies the Input Capture Polarity from @ref TIM2_ICPolarity_TypeDef.
  * @param   TIM2_ICSelection specifies the Input Capture Selection from @ref TIM2_ICSelection_TypeDef.
  * @param   TIM2_ICPrescaler specifies the Input Capture Prescaler from @ref TIM2_ICPSC_TypeDef.
  * @param   TIM2_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  * @retval None
  */
